package com.ynet.middleground.user.service.impl;

import java.util.List;

import javax.validation.Valid;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Throwables;
import com.ynet.core.common.BaseReqObjPager;
import com.ynet.core.common.ServiceResult;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.user.bean.AddTableResourceReq;
import com.ynet.middleground.user.bean.QueryBaseReq;
import com.ynet.middleground.user.bean.UpdateTableResourceReq;
import com.ynet.middleground.user.common.ErrorMessageConstant;
import com.ynet.middleground.user.dto.TableResourceDto;
import com.ynet.middleground.user.model.ChannelConfigModel;
import com.ynet.middleground.user.service.TableResourcesService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 渠道资源信息表 服务实现类
 * </p>
 *
 * @author liulx
 * @since 2020-07-01
 */
@Api(value = "渠道资源接口服务类", tags = {"渠道资源接口服务类"})
@Produces({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8", MediaType.TEXT_XML + "; " + "charset=UTF-8"})
@Path("channel-resource")
@Service(timeout = 6000, version = "0.0.1")
public class TableResourcesServiceImpl implements TableResourcesService, GenericService {

    @Autowired
    private ChannelConfigModel channelConfigModel;

    /**
     * 查询所有资源信息
     *
     * @return 所有资源列表
     */
    @Path("")
    @ApiOperation(value = "查询所有资源信息", notes = "查询所有资源信息", hidden = true)
    @Override
    public ServiceResult<List<TableResourceDto>> queryAllTableResource() {
        ServiceResult<List<TableResourceDto>> result = new ServiceResult<>();
        try {
            result.setResult(channelConfigModel.queryAllTableResource());
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "查询所有资源信息服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        return result;
    }

    /**
     * 分页查询正常状态的资源信息
     *
     * @param req 分页查询对象
     * @return 资料列表
     */
    @ApiOperation(value = "分页查询资源信息", notes = "分页查询资源信息", hidden = true)
    @Override
    public ServiceResult<List<TableResourceDto>> queryTableResourcePage(@Valid BaseReqObjPager req) {
        ServiceResult<List<TableResourceDto>> result = new ServiceResult<>();
        try {
            result.setResult(channelConfigModel.queryTableResourceByPage(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "分页查询资源信息服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        result.setPagerInfo(req.getPagerInfo());
        return result;
    }

    /**
     * 保存资源
     *
     * @param req 添加资源请求参数
     */
    @ApiOperation(value = "添加资源", notes = "添加资源", hidden = true)
    @Override
    public ServiceResult<Object> addResources(@Valid AddTableResourceReq req) {
        ServiceResult<Object> result = new ServiceResult<>();
        try {
            result.setResult(channelConfigModel.addTableResource(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "添加资源服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        return result;
    }

    /**
     * 根据资源 id 查询资源信息
     *
     * @param req 查询基础对象
     * @return 资源信息传输对象
     */
    @ApiOperation(value = "查询资源信息", notes = "查询资源信息", hidden = true)
    @Override
    public ServiceResult<TableResourceDto> queryResourceDetail(@Valid QueryBaseReq req) {
        ServiceResult<TableResourceDto> result = new ServiceResult<>();
        try {
            result.setResult(channelConfigModel.getResourceDetail(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "查询资源信息服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        return result;
    }

    /**
     * 修改资源信息
     *
     * @param req 修改资源信息参数
     * @return 结果以 success 字段为准, true 为修改成功， false 为修改失败
     */
    @ApiOperation(value = "修改资源信息", notes = "修改资源信息", hidden = true)
    @Override
    public ServiceResult<Object> modifyResource(@Valid UpdateTableResourceReq req) {
        ServiceResult<Object> result = new ServiceResult<>();
        try {
            channelConfigModel.updateTableResource(req);
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "修改资源信息服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        return result;
    }

    /**
     * 删除资源信息
     *
     * @param req 查询基础对象
     * @return 结果以 success 字段为准, true 为删除成功， false 为删除失败
     */
    @ApiOperation(value = "删除资源信息", notes = "删除资源信息", hidden = true)
    @Override
    public ServiceResult<Object> deleteResource(@Valid QueryBaseReq req) {
        ServiceResult<Object> result = new ServiceResult<>();
        try {
            channelConfigModel.deleteTableResource(req);
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "删除资源信息服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        return result;
    }

}
